package com.tju.ocean_ecology_website.dto;

import com.tju.ocean_ecology_website.entity.VolunteerServiceRecord;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 服务时长记录DTO
 * 用于返回给前端的服务时长记录数据
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class HoursRecordDTO {
    private Long id;           // 记录ID
    private String date;       // 登记日期
    private Integer hours;     // 登记时长(小时)
    private String status;     // 认证状态(verified:已认证, pending:待认证)
    private String remark;     // 备注信息

    /**
     * 从VolunteerServiceRecord实体转换为HoursRecordDTO
     * 
     * @param record 服务时长记录实体
     * @return HoursRecordDTO
     */
    public static HoursRecordDTO fromServiceRecord(VolunteerServiceRecord record) {
        if (record == null) {
            return null;
        }
        
        // 格式化日期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = record.getServiceDate() != null ? 
                dateFormat.format(record.getServiceDate()) : "";
        
        // 转换状态
        String statusStr;
        if (record.getStatus() == null) {
            statusStr = "pending";
        } else if (record.getStatus() == 1) {
            statusStr = "verified";
        } else {
            statusStr = "pending";
        }
        
        return HoursRecordDTO.builder()
                .id(record.getId())
                .date(formattedDate)
                .hours(record.getServiceHours())
                .status(statusStr)
                .remark(record.getDescription())
                .build();
    }
}
